package com.appos.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.appos.common.core.controller.BaseController;
import com.appos.common.core.domain.AjaxResult;
import com.appos.domain.Book;
import com.appos.service.MongoDbService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MongoDbController extends BaseController {

    @Autowired
    private MongoDbService mongoDbService;

    @PostMapping("/mongo/save")
    public String saveObj(@RequestBody Book book) {
        return mongoDbService.saveObj(book);
    }
//    @PostMapping("/mongo/add")
//    public AjaxResult add(@RequestBody JSONObject fromJsonObject)
//    {
//        FormInfo formInfo = new FormInfo();
//        String formName = (String) fromJsonObject.get("formName");
//        formInfo.setFormName(formName);
//        formInfo.setFormJson(fromJsonObject.toString());
//
//        //解析json中的控件信息，并存入tableFilelds，这里使用IdentityHashMap可以存储重复type的控件名
//        Map<String, String> tableFields = new IdentityHashMap<>();
//        List joArray = (List)fromJsonObject.get("column");
//        for (int i = 0; i < joArray.size(); i++) {
//            Map<String, String> map = (Map)joArray.get(i);
//            tableFields.put(map.get("type"), map.get("prop"));
//        }
//        // 给tableName添加前缀，标识为自定义
//        // String tableName = "dynamic_" + ameFromDesignJsonObject.get("tableName");
//        String tableName = (String) fromJsonObject.get("formName");
//        // mongoDbService.createAutoTask(tableName, tableFields);
//        return toAjax(mongoDbService.save(formInfo));
//    }
}
